<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   Gtk条目过滤器($)*/

!defined('DYHB_PATH')&& exit;

// 导入迭代器接口
Package::importI('IIterator','MODE');

class GtkTreeItemFilterIterator implements IIterator{

	private $_oTreeItemIterator;
	private $_arrFilters=array();
	private $_arrRegexpFilters=array();

	public function __construct(GtkTreeModel $oModel,GtkTreeIter $oStartItem=null,$nLevel=0){
		$this->_oTreeItemIterator=new GtkTreeItemIterator($oModel,$oStartItem,$nLevel);
		$this->first();
	}

	public function first(){
		$this->_oTreeItemIterator->first();
		while(!$this->_oTreeItemIterator->isDone()){
			$oItem=$this->_oTreeItemIterator->current();
			if($this->filter_($oItem)){return $oItem;}// 遇到的第一个符合条件的 行
			$this->_oTreeItemIterator->next();
		}
		return null;
	}

	public function next(){
		if($this->isDone())
			return null;
		while(!$oItem=$this->_oTreeItemIterator->next()){
			if($this->filter_($oItem)){return $oItem;}// 遇到的第一个符合条件的 行
		}
		return null;
	}

	public function current(){
		return $this->_oTreeItemIterator->current();
	}

	public function isDone(){
		return $this->_oTreeItemIterator->isDone();
	}

	public function getModel(){
		return $this->_oTreeItemIterator->getModel();
	}

	public function addFilter($nColumn,$Value,$bLogic=true){
		A::INT($nColumn);
		$oModel=$this->_oTreeItemIterator->getModel();
		A::INSTANCE($oModel,'GtkTreeModel');
		A::ASSERT_($nColumn<$oModel->get_n_columns(),G::L('参数 $nColumn(=%d)必须小于 模型的 Column数量(=%d)','gtk',null,$nColumn,$oModel->get_n_columns()));
		$this->_arrFilters[]=array('column'=>$nColumn,'value'=>$Value,'logic'=>$bLogic);
	}

	public function addRegexpFilter($nColumn, $sRegexp, $bLogic=true){
		A::INT($nColumn);
		A::STRING($sRegexp);
		$oModel=$this->_oTreeItemIterator->getModel();
		A::INSTANCE($oModel,'GtkTreeModel');
		A::ASSERT_($nColumn<$oModel->get_n_columns(),G::L('参数 $nColumn(=%d)必须小于 模型的 Column数量(=%d)','gtk',null,$nColumn,$oModel->get_n_columns()));
		$this->arrRegexpFilters[]=array('column'=>$nColumn,'regexp'=>$sRegexp,'logic'=>$bLogic);
	}

	private function filter_(GtkTreeIter $oItem){
		$oModel=$this->_oTreeItemIterator->getModel();
		A::INSTANCE($oModel,'GtkTreeModel');
		foreach($this->_arrFilters as $arrOneFilter){// 普通 过滤
			A::ASSERT_($arrOneFilter['column']<$oModel->get_n_columns(),G::L('参数 $nColumn(=%d)必须小于 模型的 Column数量(=%d)','gtk',null,$arrOneFilter['column'],$oModel->get_n_columns()));
			$Value=$oModel->get_value($oItem,$arrOneFilter['column']);
			if($arrOneFilter['logic'] and ($arrOneFilter['value']!==$Value)){return false;}
			if(!$arrOneFilter['logic'] and ($arrOneFilter['value']===$Value)){return false;}
		}
		foreach($this->_arrRegexpFilters as $arrOneFilter){// 正则式过滤
			A::ASSERT_($arrOneFilter['column']<$oModel->get_n_columns(),G::L('参数 $nColumn(=%d)必须小于 模型的 Column数量(=%d)','gtk',null,$arrOneFilter['column'],$oModel->get_n_columns()));
			$Value=$oModel->get_value($oItem,$arrOneFilter['column']);
			$bRes=preg_match($arrOneFilter['regexp'],$Value);
			if($arrOneFilter['logic'] and $bRes){return false;}
			if(!$arrOneFilter['logic'] and $bRes){return false;}
		}
		return true;
	}

}
